<?php

namespace app\admin\model\information;

use think\Db;
use think\Model;
use traits\model\SoftDelete;
use fast\Tree;
use app\common\model\Shelves;

class InventoryClass extends Model
{

    //use SoftDelete;

    

    // 表名
    protected $name = 'inventory_class';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    public static function geTreeData2(){
        $list = Db::name('inventory_class')->field('cInvCCode,cInvCName,iInvCGrade,bInvCEnd')->select();
        $treeData = array();
        foreach ($list as $k => $v) {
            $id         = $v['cInvCCode'];
            $iInvCGrade = $v['iInvCGrade'];
            
            if ($iInvCGrade == 1) {
                $pid = 0;
            } else {
                $pid = substr($id, 0, strlen($id) - 2);
            }
            $treeData[] = [
                'id' => $id,
                'name' => $v['cInvCName'],
                'pid' => $pid,
            ];
            
        }
        return $treeData;
    }
    
    public static function geTreeData(){
        //$list = Db::name('inventory_class')->field('cInvCCode,cInvCName,iInvCGrade,bInvCEnd')->select();
		$list = Db::name('inventory_class')->field('id,pid,cInvCName name')->select();
		return $list;
        $treeData = array();
        foreach ($list as $k => $v) {
            $id         = $v['cInvCCode'];
            $iInvCGrade = $v['iInvCGrade'];

            if ($iInvCGrade == 1) {
                $pid = 0;
            } else {
                $pid = substr($id, 0, strlen($id) - 2);
            }
            $treeData[] = [
                'id' => $id,
                'name' => $v['cInvCName'],
                'pid' => $pid,
            ];

        }
        return $treeData;
    }

    /**
     * @param $data
     * @param int $id
     * @param int $level
     * @param array $path
     * @param string $pid
     * @param string $children
     * @return array
     */
     public static function listToTreeByRecursion($data, $id = 0,$level = 1,$path = [],$pid = 'pid',$children = 'children'){
        $child = [];
        //循环所有数据找$id的child
        foreach ($data as $key => $datum) {
            //$datum['isLeaf'] = false;
            if ($datum[$pid] == $id) {
                //路径
                //$datum['path']  = array_merge($path,array($id));
                $path = array_merge($path,array($id));
                //当前层级
                //$datum['level'] = $level;
                $child[$datum['id']] = $datum;
                //先去掉自己，自己不可能是自己的
                unset($data[$key]);
                //递归找，并把找到的放到一个child的字段中
                $child[$datum['id']][$children] = self::listToTreeByRecursion($data, $datum['id'],$level + 1,$path);//$datum['path']
                //归来，判断是否是叶子节点
                if(empty($child[$datum['id']][$children])){
                    //$child[$datum['id']]['isLeaf'] = true;
                }
            }
        }
        return array_values($child);
    }
    
    // 获取指定 ID 下的所有子 ID
    public static function getChildIds($parentId)
    {
        // 从数据库中查询所有数据
        $classList = db('inventory_class')->field('id,pid,cInvCName')->select();
        
        // 构建树形结构
        $tree = Tree::instance();
        $tree->init($classList);
        // 递归获取指定 ID 下的所有子 ID
        $childIds = $tree->getChildrenIds($parentId,true);
        
        return $childIds;
    }
  
    //获取某个
    public static function getBusinessIdsByDepID($department_id = 0,$emp_id){
        if(!$emp_id){
            return;
        }
        $business_ids_arr = [];
        if(!$department_id){
            //没部门，也只看自己
            $business_ids_arr = db('crm_business_relation')->where([
                'emp_id' => $emp_id
            ])->column('business_id');
        }else{
            //去下级所有部门包含自己
            $departments = self::getChildIds($department_id,true);
            
            if($departments){
                //有下级部门，看整个部门
                $emp_ids = db('user')->where([
                    'isemployee'=>1,
                    'department_id' => ['IN',$departments],
                    'status' => 'normal'
                ])->column('id');
                
                if($emp_ids){
                    $business_ids_arr = db('crm_business_relation')->where([
                        'emp_id' => ['IN',$emp_ids]
                    ])->column('business_id');
                }
            }
            
            /* if(count($departments) == 1){
             //最末端部门，找出这个部门的员工
             
             $business_ids_arr = db('crm_business_relation')->where([
             'emp_id' => $emp_id
             ])->column('business_id');
             }else{
             //有下级部门，看整个部门
             $emp_ids = db('user')->where([
             'isemployee'=>1,
             'department_id' => ['IN',$departments],
             'status' => 'normal'
             ])->column('id');
             
             if($emp_ids){
             $business_ids_arr = db('crm_business_relation')->where([
             'emp_id' => ['IN',$emp_ids]
             ])->column('business_id');
             }
             } */
        }
        
        return $business_ids_arr;
    }
    
    // 添加关系
    public function addRelation($inv_class_ids_arr,$shelves_id){
        if(!$inv_class_ids_arr||!$shelves_id){
            exception('inv_class_ids_arr参数错误');
        }
        
        $shelves = Shelves::get($shelves_id);
        if(!$shelves){
            exception('找不到对应货架信息');
        }
        
        foreach ($inv_class_ids_arr as $inv_class_id){
            //后增加
            db('shelves_invclass')->insert([
                'shelves_id' => $shelves_id,
                'inv_class_id' => $inv_class_id
            ]);
        }
    }
}
